$(function () {
    layui.use(['table','layer'],function () {
        var table = layui.table;
        var layer = layui.layer;
        table.render({
            elem:'#LAY_table_driver',
            url:'/pages/back/log/driver',
            page:true,
            limit:10,
            toolbar: '#toolbarDemo',//开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print'],
            cols:[[
                {type: 'checkbox', fixed: 'left'},
                {field: 'ip', title: 'IP',sort: true},
                {field: 'url', title: 'URL',sort: true},
                {field: 'description', title: '描述'},
                {field: 'type', title: '操作者',sort: true},
                {field: 'httpMethod', title: 'httpMethod',sort: true},
                {field: 'classMethod', title: 'classMethod'},
                {field: 'requestParams', title: '请求参数'},
                {
                    field: 'createDate',
                    title: '操作时间',
                    templet: "<div>{{layui.util.toDateString(d.createDate,'yyyy-MM-dd HH:mm:ss')}}</div>",
                    sort:true
                },
                {field:'right',title:'操作',templet:"#barOption"}
            ]],
            id: 'testReload'
        });

        var $ = layui.$, active = {
            reload: function(){
                var createDate = $('#demoReload').serialize();

                //执行重载
                table.reload('testReload', {
                    url:'/pages/back/log/findByCreateDate?'+createDate,
                    where: {
                        key: {
                            createDate: createDate
                        }
                    },
                });
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //单个删除
        table.on('tool(driver)', function(obj){
            var data = obj.data;
            var id = data.id
            layer.confirm('删除?',function () {
                $.ajax({
                    url:'/pages/back/logger/delete?'+id,
                    type: 'POST',
                    data: {"id":id},
                    success:function (data) {
                        if(data == "success"){ // 表示成功
                            layer.msg('删除成功',{time:1000,icon:1,end:function () {
                                    // 登录成功之后跳转到后台首页
                                    window.location.href = "/pages/back/log/driverPre";
                                }})
                        }else if(data == "isEmpty"){ // 表示验证失败
                            layer.msg('数据不能为空或者数据格式错误',{time:1000,icon:2})
                        }else{
                            layer.msg('删除失败',{time:1000,icon:2})
                        }
                    }
                })
            })
        });
        //批量删除
        $("#delAll").click(function () {
            var checkStatus = table.checkStatus('testReload'),
                data = checkStatus.data,
                id = "";
            if (data.length > 0) {
                for (var i in data) {
                    id += data[i].id + ",";
                }
                layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                    $.ajax({
                        type:'post',
                        data:{'ids':id},
                        url:'/pages/back/log/delAll',
                        success:function (data) {
                            if(data == "success"){ // 表示成功
                                layer.msg('删除成功',{time:1000,icon:1,end:function () {
                                        // 登录成功之后跳转到后台首页
                                        window.location.href = "/pages/back/log/driverPre";
                                    }})
                            }else{
                                layer.msg('删除失败',{time:1000,icon:2})
                            }
                        }
                    })
                })
            } else {
                layer.msg("请选择需要删除的用户");
            }
        });

    })
})